/**
 * @Project POL3Cay
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Feb 23, 2011
 * @Createtime 11:44:50 AM
 */

package renders
{
	import assets.CardRender;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;

	import models.card.BaseHand;
	import models.card.Card;

	public class BaseHandRenderer extends Sprite
	{
		protected var _numCards:int = 0;

		private var _cardsSetRender:Vector.<CardRender>;

		protected var _openAngle:int = 0;

		protected var _radius:int = 0;

		public function BaseHandRenderer()
		{
			super();

			_cardsSetRender = new Vector.<CardRender>();

			for (var i:int = 0; i < 52; i++)
			{
				_cardsSetRender[i] = null;
			}
		}

		/**
		 * index from 1
		 * _cardsSetRender index from 0
		 */
		public function receiveCard(card:Card, index:int):void
		{
			index--;

			if (_cardsSetRender[index] == null)
			{
				_cardsSetRender[index] = new CardRender();
				_cardsSetRender[index].visible = false;
				addChild(_cardsSetRender[index]);

				_numCards++;
			}

			_cardsSetRender[index].setCard(card.value, card.suit);
		}

		public function set handCards(cardsSet:Vector.<Card>):void
		{
			_numCards = 0;

			var card:Card;

			for each (card in cardsSet)
			{
				receiveCard(card, _numCards);
			}
		}

		public function render(openAngle:int = 150, radius:int = 180):void
		{
			var i:int = 1;
			var card:CardRender;

			var stepAngle:Number = openAngle / (_numCards + 1);
			var offsetAngle:Number = (openAngle - 180) / 2; // to center hand card

			for each (card in _cardsSetRender)
			{
				if (card != null)
				{
					card.y = radius * Math.sin((offsetAngle + i * stepAngle - openAngle) / 180 * Math.PI);
					card.x = radius * Math.cos((offsetAngle + i * stepAngle - openAngle) / 180 * Math.PI);
					card.rotationZ = (90 + offsetAngle + i * stepAngle - openAngle);

					i++;
				}
			}


			_radius = radius;
			_openAngle = openAngle;
		}

		public function reset():void
		{
			_radius = 100;
			_openAngle = 0;
		}

		public function get radius():int
		{
			return _radius;
		}

		public function set radius(value:int):void
		{
			if (_radius != value)
			{
				render(_openAngle, _radius);

				_radius = value;
			}
		}

		public function get openAngle():int
		{
			return _openAngle;
		}

		public function set openAngle(value:int):void
		{
			if (_openAngle != value)
			{
				render(_openAngle, _radius);

				_openAngle = value;
			}
		}

		public function get cardsSetRender():Vector.<CardRender>
		{
			return _cardsSetRender;
		}
	}
}